package com.pj.compose.ui.page.widget

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetLayout
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material.rememberModalBottomSheetState
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.*
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.pj.compose.R
import com.pj.compose.common.config.StringExt.toast
import com.pj.compose.model.simpleItems
import com.pj.compose.ui.compose.AppTopBarColumn
import com.pj.compose.ui.compose.BottomGridContent
import com.pj.compose.ui.compose.BottomListSheetContent
import com.pj.compose.ui.compose.SimpleListView
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun BottomSheetsPage(navHostController: NavController = rememberNavController()) {
    var clickIndex by remember {
        mutableStateOf(0)
    }
    val bottomSheetState =
        rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)
    val scope = rememberCoroutineScope()
    val hideBottomSheet: () -> Unit = {
        scope.launch {
            bottomSheetState.hide()
        }
    }
    val shortItems = simpleItems.subList(0, 5)
    val itemClick: (Int) -> Unit = {
        hideBottomSheet()
        "点击了: ${simpleItems[it % simpleItems.size]}".toast()
    }
    val shareItems = listOf(
        "分享到微信",
        "分享到朋友圈",
        "分享到微博",
        "分享到私信",
        "保存到本地"
    )

    ModalBottomSheetLayout(
        sheetState = bottomSheetState,
        sheetShape = RoundedCornerShape(topStart = 10.dp, topEnd = 10.dp),
        sheetBackgroundColor = MaterialTheme.colorScheme.surface,
        sheetContent = {
            when (clickIndex) {
                0 -> BottomListSheetContent(items = shortItems, onItemClick = itemClick)
                1 -> BottomListSheetContent(
                    items = shortItems,
                    painters = listOf(
                        painterResource(id = R.mipmap.icon_tabbar_lab),
                        painterResource(id = R.mipmap.icon_tabbar_lab_selected),
                        painterResource(id = R.mipmap.icon_tabbar_util),
                        painterResource(id = R.mipmap.icon_tabbar_util_selected)
                    ),
                    onItemClick = itemClick
                )
                2 -> BottomListSheetContent(
                    items = shortItems,
                    onItemClick = itemClick,
                    horizontalArrangement = Arrangement.Center
                )
                3 -> BottomListSheetContent(
                    title = "With Title",
                    items = shortItems,
                    horizontalArrangement = Arrangement.Center,
                    onItemClick = itemClick
                )
                4 -> BottomListSheetContent(
                    title = "With Cancel Btn",
                    items = shortItems,
                    onCancel = hideBottomSheet,
                    horizontalArrangement = Arrangement.Center,
                    onItemClick = itemClick
                )
                5 -> BottomListSheetContent(items = simpleItems, onItemClick = itemClick)
                6 -> BottomListSheetContent(
                    items = simpleItems,
                    showMark = true,
                    onItemClick = itemClick
                )
                7 -> BottomGridContent(
                    items = shareItems,
                    painters = listOf(
                        painterResource(id = R.mipmap.icon_more_operation_share_chat),
                        painterResource(id = R.mipmap.icon_more_operation_share_friend),
                        painterResource(id = R.mipmap.icon_more_operation_share_weibo),
                        painterResource(id = R.mipmap.icon_more_operation_share_moment),
                        painterResource(id = R.mipmap.icon_more_operation_save)
                    ),
                    onCancel = hideBottomSheet,
                    onItemClick = {
                        hideBottomSheet()
                        shareItems[it].toast()
                    }
                )
            }
        }) {
        AppTopBarColumn(
            title = "BottomSheet",
//            modifier = Modifier.padding(it),
            backClick = { navHostController.popBackStack() }) {
            SimpleListView(
                items = listOf(
                    "Simple",
                    "With Icon",
                    "Gravity Center",
                    "With Title",
                    "With Cancel Btn",
                    "Many Items",
                    "With Mark",
                    "Bottom Sheet Grid"
                )
            ) {
                clickIndex = it
                scope.launch {
                    bottomSheetState.show()
                }
            }
        }
    }
}



@Preview(showBackground = true)
@Composable
private fun BottomSheetsPagePrev() {
    BottomSheetsPage()
}